package cn.com.fuhuitong.main.host.ui.activity

import android.annotation.SuppressLint
import android.webkit.WebSettings
import android.webkit.WebViewClient
import androidx.lifecycle.Observer
import cn.com.fuhuitong.R
import cn.com.fuhuitong.app.AppConstance
import cn.com.fuhuitong.aroute.ARouteConstance
import cn.com.fuhuitong.entity.MainBean
import cn.com.fuhuitong.entity.WebConfig
import cn.com.fuhuitong.http.HttpResponseStatus
import cn.com.fuhuitong.main.base.ViewModeActivity
import cn.com.fuhuitong.main.host.vm.MainViewModel
import cn.com.fuhuitong.utils.WebViewUtils
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.RegexUtils
import kotlinx.android.synthetic.main.activity_web_view.*

@Route(path = ARouteConstance.ACTIVITY_URL_WEB_VIEW)
class WebViewActivity : ViewModeActivity<MainViewModel>() {

    override fun viewModel(): MainViewModel = createViewModel()

    override val layoutResId: Int
        get() = R.layout.activity_web_view

    @SuppressLint("SetJavaScriptEnabled,JavascriptInterface")
    override fun initView() {

        image_web_view_back.setOnClickListener {
            this.finish()
        }

        web_view.webViewClient = WebViewClient()
        web_view.settings.domStorageEnabled = true
        web_view.settings.databaseEnabled = true
        web_view.settings.javaScriptEnabled = true
        web_view.settings.useWideViewPort = true
        web_view.settings.cacheMode = WebSettings.LOAD_NO_CACHE
        web_view.settings.layoutAlgorithm = WebSettings.LayoutAlgorithm.NARROW_COLUMNS
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
            web_view.settings.mixedContentMode = WebSettings.MIXED_CONTENT_ALWAYS_ALLOW
        }
    }

    override fun initViewMode() {

        viewModel.configDataLiveData.observe(this, Observer { response ->
            handlerResponseLoading(response.status, response.statusTip)
            if (response.status != HttpResponseStatus.Status.SUCCESS)
                return@Observer
            response.response?.data?.let {
                val mainBean: MainBean<*, *> =
                    intent?.getSerializableExtra(AppConstance.BUNDLE_WEB_VIEW) as? MainBean<*, *>
                        ?: return@let
                when (mainBean.type as WebConfig) {
                    WebConfig.HELP_CENTER -> {
                        WebViewUtils.loadFullScreenHtml(web_view, it.help_center)
                    }
                    WebConfig.USER_AGREE -> {
                        WebViewUtils.loadFullScreenHtml(web_view, it.user_register)
                    }
                    WebConfig.PRIVACY_AGREE -> {
                        WebViewUtils.loadFullScreenHtml(web_view, it.privacy_policy)
                    }
                    WebConfig.MASSAGE_CHAIR -> {
                        WebViewUtils.loadFullScreenHtml(web_view, it.massage_chair)
                    }
                    WebConfig.PUBLIC_CHARITY -> {
                        WebViewUtils.loadFullScreenHtml(web_view, it.public_charity_commitment)
                    }
                    WebConfig.STORE_CONTECT -> {
                        WebViewUtils.loadFullScreenHtml(web_view, it.store_contect)
                    }
                    WebConfig.WITHDRAW_DESCRIPTION -> {
                        WebViewUtils.loadFullScreenHtml(web_view, it.cash_rule)
                    }
                }
            }
        })
    }

    override fun initData() {

        val mainBean: MainBean<*, *> =
            intent?.getSerializableExtra(AppConstance.BUNDLE_WEB_VIEW) as? MainBean<*, *>
                ?: return
        when (mainBean.type as WebConfig) {
            WebConfig.UNKNOWN -> {
                text_web_view_title.text = mainBean.type.desc
                val webUrl: String? = mainBean.data as? String
                if (webUrl != null) {
                    if (RegexUtils.isURL(webUrl)) {
                        web_view.loadUrl(webUrl)
                    } else {
                        WebViewUtils.loadFullScreenHtml(web_view, webUrl)
                    }
                } else {
                    toast("地址NULL,未设置")
                }
            }
            WebConfig.HELP_CENTER -> {
                text_web_view_title.text = mainBean.type.desc
                viewModel.configValue(arrayOf("help_center"))
            }
            WebConfig.USER_AGREE -> {
                text_web_view_title.text = mainBean.type.desc
                viewModel.configValue(arrayOf("user_register"))
            }
            WebConfig.PRIVACY_AGREE -> {
                text_web_view_title.text = mainBean.type.desc
                viewModel.configValue(arrayOf("privacy_policy"))
            }
            WebConfig.MASSAGE_CHAIR -> {
                text_web_view_title.text = mainBean.type.desc
                viewModel.configValue(arrayOf("massage_chair"))
            }
            WebConfig.PUBLIC_CHARITY -> {
                text_web_view_title.text = mainBean.type.desc
                viewModel.configValue(arrayOf("public_charity_commitment"))
            }
            WebConfig.STORE_CONTECT -> {
                text_web_view_title.text = mainBean.type.desc
                viewModel.configValue(arrayOf("store_contect"))
            }
            WebConfig.WITHDRAW_DESCRIPTION -> {
                text_web_view_title.text = mainBean.type.desc
                viewModel.configValue(arrayOf("cash_rule"))
            }
        }
    }


}